home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / WINSOCK.PAK / SOCKDEMO.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  6KB  |  182 lines

  1. /*-----------------------------------------------------------------------*\
  2. | OWLSock Demo For Windows v1.0                                           |
  3. --------------------------------------------------------------------------|
  4. | Written By:  Paul Pedriana                                              |
  5. | Date:        May 7, 1995.                                               |
  6. | Copyright:   Copyright (c) 1995 by Paul Pedriana.  All Rights Reserved. |
  7. | UserID(s):   70541,3223                                                 |
  8. |              70541.3223@compuserve.com                                  |
  9. --------------------------------------------------------------------------|
  10. | This OWLSock demo is an application that demonstrates some features     |
  11. | of OWLSock.  It uses only asynchronous (non-blocking) Winsock calls,    |
  12. | and uses OWLSock socket 'external' notification rather than internal    |
  13. | notification.  External notification is the way most Winsock apps do    |
  14. | FD_XXX notifications; see the OWLSock docs for more info.               |
  15. --------------------------------------------------------------------------|
  16. | Notes on this module:                                                   |
  17. |    This is the application module.  It essentially fields menu choices  |
  18. | and makes dialogs out of them.  The dialogs all the real work.          |
  19. \*-----------------------------------------------------------------------*/
  20.  
  21. #include <owl/pch.h>
  22. #if !defined(OWL_WINSOCK_H)
  23. # include <owl/winsock.h>
  24. #endif
  25. #include <stdio.h>
  26. #include "sockdemo.h"
  27. #include "sockbout.h" //Used by CmHelpAbout().  The about dialog
  28. #include "dlgaddr.h"  //Used by CmConvertAddress(). The Name to Address conversion dialog
  29. #include "dlgsrvc.h"  //Used by CmConvertService(). The Service to Port conversion dialog
  30. #include "dlgdgrm.h"  //Used by CmSendDatagrams(). The Datagram sending dialog
  31. #include "dlgstrm.h"  //Used by CmStreamConnect()/CmStreamListen. The Stream sending dialog
  32. #include "dlglstn.h"  //Used by CmStreamListen(). The Listen dialog
  33.  
  34. //
  35. // Our one global:  In this demo app, it doesn't need to be global,
  36. //  but you want it that way in other apps, as there generally need be
  37. //  only one SocketManager;
  38. //
  39. TSocketManager* gSocketManager;
  40.  
  41. DEFINE_RESPONSE_TABLE1(SockDemoApp, TApplication)
  42.   EV_COMMAND(CM_GET_WSADATA, CmGetWSAData),
  43.   EV_COMMAND(CM_GET_MY_ADDRESS, CmGetMyAddress),
  44.   EV_COMMAND(CM_CONVERT_SERVICE, CmConvertService),
  45.   EV_COMMAND(CM_CONVERT_ADDRESS, CmConvertAddress),
  46.   EV_COMMAND(CM_SEND_DATAGRAMS, CmSendDatagrams),
  47.   EV_COMMAND(CM_STREAM_LISTEN, CmStreamListen),
  48.   EV_COMMAND(CM_STREAM_CONNECT, CmStreamConnect),
  49.   EV_COMMAND(CM_SHOW_SOCKET_ERROR, CmShowSocketError),
  50.   EV_COMMAND(CM_HELPABOUT, CmHelpAbout),
  51.   EV_COMMAND(CM_HELP_HOW, CmHelpHow),
  52. END_RESPONSE_TABLE;
  53.  
  54.  
  55. SockDemoApp::SockDemoApp()
  56. :
  57.   TApplication("OWLSock demo")
  58. {
  59.   gSocketManager = new TSocketManager();
  60. }
  61.  
  62.  
  63. SockDemoApp::~SockDemoApp()
  64. {
  65.   delete gSocketManager;
  66. }
  67.  
  68.  
  69. void SockDemoApp::InitMainWindow()
  70. {
  71.   if(nCmdShow != SW_HIDE)
  72.     nCmdShow = (nCmdShow != SW_SHOWMINNOACTIVE) ? SW_SHOWNORMAL : nCmdShow;
  73.  
  74.   TFrameWindow *frame = new TFrameWindow((TWindow*)NULL, GetName());
  75.   frame->SetIcon(this, IDI_SDIAPPLICATION);
  76.   frame->AssignMenu(MENU_MAIN);
  77.   frame->Attr.AccelTable = MENU_MAIN;
  78.   SetMainWindow(frame);
  79.   frame->SetMenuDescr(TMenuDescr(MENU_MAIN));
  80. }
  81.  
  82. void SockDemoApp::CmGetWSAData()
  83. {
  84.   char output[256];
  85.   sprintf(output,
  86.           "Major Version: %d\n"
  87.           "Minor Version: %d\n"
  88.           "Max Sockets  : %d\n"
  89.           "Description:  \n"
  90.           "%s\n\n"
  91.           "SystemStatus:  \n"
  92.           "%s",
  93.            gSocketManager->GetMajorVersion(),
  94.            gSocketManager->GetMinorVersion(),
  95.            gSocketManager->GetMaxSocketsAvailable(),
  96.            gSocketManager->GetDescription(),
  97.            gSocketManager->GetSystemStatus());
  98.   GetMainWindow()->MessageBox(output, "Sockets Info", MB_OK);
  99. }
  100.  
  101. void SockDemoApp::CmGetMyAddress()
  102. {
  103.   char szOutput[148];
  104.   char szName[128];
  105.   char szAddress[20];
  106.   THostInfoManager myHostInfoManager;
  107.  
  108.   myHostInfoManager.GetHostName(szName, 128);
  109.   myHostInfoManager.GetHostAddress(szAddress, szName);  //Will this block if it is my address?  Doesn't seem to.
  110.   wsprintf(szOutput, "This computer's name:\n"
  111.                "%s\n\n"
  112.                "This computer's address:\n"
  113.                "%s", szName, szAddress);
  114.   GetMainWindow()->MessageBox(szOutput, "This Computer's Info", MB_OK);
  115. }
  116.  
  117. void SockDemoApp::CmConvertService()
  118. {
  119.   DlgConvertService* demoDlgConvertService = new DlgConvertService(GetMainWindow());
  120.   demoDlgConvertService->Create();
  121. }
  122.  
  123. void SockDemoApp::CmConvertAddress()
  124. {
  125.   DlgConvertAddress* demoDlgConvertAddress = new DlgConvertAddress(GetMainWindow());
  126.   demoDlgConvertAddress->Create();
  127. }
  128.  
  129. void SockDemoApp::CmSendDatagrams()
  130. {
  131.   DlgSendDatagram* demoDlgSendDatagram = new DlgSendDatagram(GetMainWindow());
  132.   demoDlgSendDatagram->Create();
  133. }
  134.  
  135. void SockDemoApp::CmStreamListen()
  136. {
  137.   DlgListen* demoDlgListen = new DlgListen(GetMainWindow());
  138.   demoDlgListen->Create();
  139. }
  140.  
  141.  
  142. void SockDemoApp::CmStreamConnect()
  143. {
  144.   DlgSendStream* demoDlgSendStream = new DlgSendStream(GetMainWindow());
  145.   demoDlgSendStream->Create();
  146. }
  147.  
  148. void SockDemoApp::CmShowSocketError()
  149. {
  150.   GetMainWindow()->MessageBox(TSocketError(WSAEWOULDBLOCK).AppendError("You forgot to make the socket non-blocking."), "Error (not real)", MB_OK);
  151. }
  152.  
  153.  
  154. void SockDemoApp::CmHelpAbout()
  155. {
  156.   DlgSockDemoAbout(GetMainWindow()).Execute();
  157. }
  158.  
  159.  
  160. void SockDemoApp::CmHelpHow(){
  161.   GetMainWindow()->MessageBox(
  162.                     "You use the stream and datagram socket parts of this demo"
  163.                     " by running a copy of this demo on multiple computers"
  164.                     " at a time and use the stream and datagram dialog boxes"
  165.                     " to make connections between them.",
  166.                     "How to use this demo", MB_OK);
  167. }
  168.  
  169. //
  170. //
  171. //
  172. int OwlMain (int , char* [])
  173. {
  174.   if (!TWinSock::IsAvailable()) {
  175.     ::MessageBox(0, "WinSock cannot be initialized.", "Error", MB_OK | MB_ICONSTOP);
  176.     return -1;
  177.   }
  178.  
  179.   SockDemoApp app;
  180.   return app.Run();
  181. }
  182.